package com.yata.utils.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by hecy on 2017/12/27.
 */
public class Tools {

    public String MD5(String sourceStr, int leng) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
            if (leng == 16) {
                result = buf.toString().substring(8, 24);
            }
            System.out.println("MD5(" + sourceStr + ",32) = " + result);
            System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        }
        return result;
    }

    /**
     * 把给定的字符串全部修改成大写字母
     * 遇到空格的用下划线代替
     *
     * @param value
     */
    public static String coverToUpWord(String value) {
        int len = value.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            String tem = value.substring(i, i + 1);
            if (" ".equals(tem)) {
                sb.append("_");
            } else {
                sb.append(tem.toUpperCase());
            }
        }
        return sb.toString();
    }

    /**
     * 把给定的字符串全部修改成小写写字母
     * 遇到空格的用下划线代替
     *
     * @param value
     */
    public static String coverToLower(String value) {
        int len = value.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            String tem = value.substring(i, i + 1);
            if (" ".equals(tem)) {
                sb.append("");
            } else {
                sb.append(tem.toLowerCase());
            }
        }
        return sb.toString();


    }
    /**
     * 把给定的字符串全部修改成驼峰
     * @param value
     */
    public static String coverToHump(String value) {
        StringBuffer sb = new StringBuffer();
        String [] datas = value.split(" ");

        for (int i = 0; i < datas.length; i++) {
            String data = datas[i];
            sb.append(data.substring(0, 1).toUpperCase() + data.substring(1));
        }
        String dataFinal = sb.toString();

        return dataFinal.substring(0, 1).toLowerCase() + dataFinal.substring(1);
    }


    /**
     * 把给定的字符串全部修改成小写写字母
     * 遇到空格的用下划线代替
     *
     * @param value
     */
    public static String cover(String value) {
        int len = value.trim().length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            String tem = value.substring(i, i + 1);
            if (" ".equals(tem)) {
                sb.append("");
            } else {
                sb.append(tem.toLowerCase());
            }
        }
        String data = sb.toString();

        return data.substring(0, 1).toUpperCase() + data.substring(1);
    }


    public static void main(String[] args) throws Exception {
        String data = "code sdas ddd";
        System.out.println( coverToHump(data));
    }
}
